package com.power.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.Prod;
import com.power.model.Result;
import com.power.service.MemberCollectionService;
import com.power.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 会员收藏商品控制层
 * @Author dingchunlin
 * @Date 2024/07/21 13:26
 * @Version 1.0.0
 */
@Api(tags = "会员收藏商品接口管理")
@RestController
@RequestMapping("/p/collection")
public class MemberCollectionController {

    @Autowired
    private MemberCollectionService memberCollectionService;


    /**
     * 查询会员收藏商品的数量
     * @return Result<Integer>
     */
    @ApiOperation(value = "查询会员收藏商品的数量")
    @GetMapping("/count")
    public Result<Long> loadMemberCollectionProdCount() {
        Long count = memberCollectionService.queryMemberCollectionProdCount();
        return Result.success(count);
    }

    /**
     * 查询会员收藏商品的列表
     * @param current 当前页
     * @param size 每页数量
     * @return Result<Page<Prod>>
     */
    @ApiOperation(value = "查询会员收藏商品的列表")
    @GetMapping("/prods")
    public Result<Page<Prod>> loadMemberCollectionProdsPage(@RequestParam Long current,
                                                            @RequestParam Long size) {
        Page<Prod> page = memberCollectionService.queryMemberCollectionProdsPageByOpenId(AuthUtils.getMemberOpenId(), current, size);
        return Result.success(page);
    }


    /**
     * 根据商品ID查询是否收藏
     * @param prodId 商品ID
     * @return Result<Boolean>
     */
    @ApiOperation(value = "根据商品ID查询是否收藏")
    @GetMapping("/isCollection")
    public Result<Boolean> isCollection(@RequestParam Long prodId) {
        Boolean isCollection = memberCollectionService.isCollection(AuthUtils.getMemberOpenId(), prodId);
        return Result.success(isCollection);
    }

    /**
     * 用户收藏或取消收藏商品
     * @param prodId 商品ID
     * @return Result<String>
     */
    @ApiOperation(value = "用户收藏或取消收藏商品")
    @PostMapping("/addOrCancel")
    public Result<String> addOrCancelProdCollection(@RequestBody Long prodId) {
        memberCollectionService.addOrCancelProdCollection(AuthUtils.getMemberOpenId(), prodId);
        return Result.success("操作成功");
    }
}
